﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Pesico.Forms.EventReceiver
{
    public class ApproverWorkflowMappingEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            //
            UpdateTitle(properties);
        }

        private static void UpdateTitle(SPItemEventProperties properties)
        {
            string creator = properties.AfterProperties["Creator"].ToString();
            int creatorId = 0;
            int.TryParse(creator, out creatorId);
            SPUser user = null;
            if (creatorId == 0)
                user = properties.Web.EnsureUser(creator.Split(";#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]);
            else
                user = properties.Web.SiteUsers.GetByID(int.Parse(creator));
            properties.AfterProperties["Title"] = user.Name;
        }

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
            //
            //UpdateTitle(properties);
        }
    }
}
